﻿Imports System.Runtime.InteropServices

Public Class panelVideo
    Inherits System.Windows.Forms.Panel

    'timer du doubleclick
    Dim WithEvents TimerDoubleclick As New Timer
    'booleen pour savoir si il y a deja un click
    Dim firstclick As Boolean = False
    Dim droitier As Boolean = True

    'recuperation du temps limite du doubleclick
    <DllImport("user32.dll")> _
    Public Shared Function GetDoubleClickTime() As Integer
    End Function

    'recuperation de l'inversion des boutons de la souris
    '0   -> pas dinversion (droitier)
    '>0  -> inversion (gaucher) 
    <DllImport("user32.dll")> _
    Public Shared Function GetSystemMetrics(ByVal swapbutton As Integer) As Integer
    End Function

    Dim buttons As New MouseButtons
    Dim PointDown As Point
    Const WM_PARENTNOTIFY As Integer = &H210
    Public Shadows Event MouseDown(ByVal Sender As Object, ByVal e As MouseEventArgs)
    Public Shadows Event MouseDoubleClick(ByVal Sender As Object, ByVal e As MouseEventArgs)


    'Public Event Desc(ByVal m As System.Windows.Forms.Message)
    Public Enum MouseEvent
        WM_LBUTTONDOWN = &H201 'bouton gauche down
        WM_RBUTTONDOWN = &H204 'bouton left down
        WM_MBUTTONDOWN = &H207 'bouton Middle down
    End Enum


    'initialisation du timer à la construction du panel
    Public Sub New()
        TimerDoubleclick.Interval = GetDoubleClickTime()
        TimerDoubleclick.Enabled = True
        droitier = (GetSystemMetrics(23) = 0)
    End Sub

    Private Sub TimerDoubleClick_Tick() Handles TimerDoubleclick.Tick
        firstclick = False
        TimerDoubleclick.Stop()
        RaiseEvent MouseDown(Me, New MouseEventArgs(buttons, 2, PointDown.X, PointDown.Y, 0))
    End Sub


    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

        If m.Msg = WM_PARENTNOTIFY Then
            Dim testclick As Integer = m.WParam.ToInt32
            If testclick = MouseEvent.WM_LBUTTONDOWN Or _
                testclick = MouseEvent.WM_MBUTTONDOWN Or _
                testclick = MouseEvent.WM_RBUTTONDOWN Then

                buttons = GetMouseButtonFlag(testclick)
                PointDown = New Point(m.LParam.ToInt32)

                If (buttons = MouseButtons.Left And droitier) OrElse (buttons = MouseButtons.Right And Not (droitier)) Then
                    If firstclick Then
                        firstclick = False
                        TimerDoubleclick.Stop()
                        RaiseEvent MouseDoubleClick(Me, New MouseEventArgs(buttons, 1, PointDown.X, PointDown.Y, 0))
                    Else
                        firstclick = True
                        TimerDoubleclick.Start()
                    End If
                Else
                    firstclick = False
                    TimerDoubleclick.Stop()
                    RaiseEvent MouseDown(Me, New MouseEventArgs(buttons, 1, PointDown.X, PointDown.Y, 0))
                End If
            End If
        ElseIf m.Msg = MouseEvent.WM_LBUTTONDOWN Or _
                 m.Msg = MouseEvent.WM_MBUTTONDOWN Or _
                 m.Msg = MouseEvent.WM_RBUTTONDOWN Then
            'Dim buttons As MouseButtons
            If m.WParam.ToInt32 = &H1 Then
                buttons = MouseButtons.Left
            End If
            If m.WParam.ToInt32 = &H2 Then
                buttons = MouseButtons.Right
            End If
            If m.WParam.ToInt32 = &H10 Then
                buttons = MouseButtons.Middle
            End If
            Dim PointDown As New Point(m.LParam.ToInt32)
            If (buttons = MouseButtons.Left And droitier) OrElse (buttons = MouseButtons.Right And Not (droitier)) Then
                If firstclick Then
                    firstclick = False
                    TimerDoubleclick.Stop()
                    RaiseEvent MouseDoubleClick(Me, New MouseEventArgs(buttons, 1, PointDown.X, PointDown.Y, 0))
                Else
                    firstclick = True
                    TimerDoubleclick.Start()
                End If
            Else
                firstclick = False
                TimerDoubleclick.Stop()
                RaiseEvent MouseDown(Me, New MouseEventArgs(buttons, 1, PointDown.X, PointDown.Y, 0))
            End If

        End If

        'RaiseEvent Desc(m)
        MyBase.WndProc(m)
    End Sub

    Private Shared Function GetMouseButtonFlag(ByVal wParam As Integer) As MouseButtons
        Select Case (wParam)
            Case MouseEvent.WM_LBUTTONDOWN
                Return MouseButtons.Left
            Case MouseEvent.WM_MBUTTONDOWN
                Return MouseButtons.Middle
            Case MouseEvent.WM_RBUTTONDOWN
                Return MouseButtons.Right
            Case Else
                Return MouseButtons.None
        End Select
    End Function
End Class
